import 'dart:core';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class CustomFormTextField extends StatefulWidget {
  final bool? enable;
  final String? hintText;
  final String? text;
  final TextAlign textAlign;
  final bool isShowClearBtn;
  final TextEditingController controller;
  final TextInputType? textInputType;
  final TextInputAction? textInputAction;
  final List<TextInputFormatter>? inputFormatters;
  final bool obscureText;

  const CustomFormTextField({
    Key? key,
    this.enable = true,
    this.text,
    this.hintText,
    this.isShowClearBtn = true,
    this.textAlign = TextAlign.start,
    required this.controller,
    this.textInputType,
    this.textInputAction,
    this.inputFormatters,
    this.obscureText = false,
  }) : super(key: key);

  @override
  State<StatefulWidget> createState() => _CustomFormTextFieldState();
}

class _CustomFormTextFieldState extends State<CustomFormTextField> {
  bool isTextEmpty = true;

  @override
  Widget build(BuildContext context) {
    return TextField(
      obscureText: widget.obscureText,
      keyboardType: widget.textInputType,
      inputFormatters: widget.inputFormatters,
      textInputAction: widget.textInputAction,
      enabled: widget.enable,
      textAlign: widget.textAlign,
      controller: widget.controller,
      style: TextStyle(fontSize: 32.sp),
      onChanged: (v) => setState(() {
        isTextEmpty = widget.controller.text.isEmpty;
      }),
      decoration: InputDecoration(
        suffixIcon: widget.isShowClearBtn
            ? isTextEmpty
                ? null
                : TextButton(
                    onPressed: () {
                      setState(() {
                        widget.controller.clear();
                        isTextEmpty = true;
                      });
                    },
                    child: Image(
                      width: 36.w,
                      height: 36.h,
                      image: const AssetImage(
                          "assets/images/icon_text_field_clear.png"),
                    ),
                  )
            : null,
        border: InputBorder.none,
        hintText: widget.hintText,
        hintStyle: const TextStyle(
          color: Color(0xFF999999),
        ),
      ),
    );
  }
}
